<template>
  <div class="risk app-container">
    <div class="mb-20">
      <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
    </div>

    <el-table v-loading="loading" :data="list" style="width: 100%;">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="最小风控金额" align="center" prop="min" />
      <el-table-column label="最大风控金额" align="center" prop="max" />
      <el-table-column label="风控概率" align="center" prop="winRatio" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:user:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:user:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"  @pagination="getList"/>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="590px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="20">
            <el-form-item label="风控概率" prop="winRatio">
              <el-input type="number" v-model.number="form.winRatio" placeholder="请输入风控概率"   :max="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="最小风控金额" prop="min">
              <el-input type="number" v-model="form.min" placeholder="请输入最小风控金额" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="最大风控金额" prop="max">
              <el-input type="number" v-model="form.max" placeholder="请输入最大风控金额" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="risk-tips ">
        <div>
          <strong>说明 ：</strong> 输入金额区间，设置风控概率会根据此概率盈亏，不在此区间则不受风控影响
        </div>
        <div>
          <strong>格式 ：</strong> 风控最小金额和风控最大金额 例如： 最小金额100 - 最大金额1000 概率0.8 ，100至1000赢的概率为80%
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getWindControlList, putWindControl, postWindControl, delWindControl } from "@/api/system/risk";

export default {
  name: "Online",
  
  data() {
    const equalToWinRatio = (rule, value, callback) => {
      if (value > 1) {
        callback(new Error("风控概率不能超过1"));
      } else {
        callback();
      }
    };
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
     
      title: '',
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        max: undefined,
        min: undefined,
        winRatio: undefined,
      },
      // 表单校验
      rules: {
        winRatio: [
          { required: true, message: "风控概率不能为空", trigger: "blur" },
          { required: true, validator: equalToWinRatio, trigger: "blur" }
        ],
        min: [
          { required: true, message: "最小风控金额不能为空", trigger: "blur" }
        ],
        max: [
          { required: true, message: "最大风控金额不能为空", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      getWindControlList(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        winRatio: undefined,
        min: undefined,
        max: undefined,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "增加全局风控";
      // getUser().then(response => {
      //   // this.postOptions = response.posts;
      //   // this.roleOptions = response.roles;

      // });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            putWindControl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            postWindControl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
       /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = {
        id: row.id,
        winRatio: row.winRatio,
        min: row.min,
        max: row.max,
      };
      this.open= true;
      this.title = '修改全局风控'
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除风控id为"' + row.id + '"的数据项？').then(function () {
        return delWindControl(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};
</script>
<style>
.risk-tips {
  background: rgba(238, 88, 88, 0.2);
  padding: 10px;
  border-radius: 6px;
  color: rgb(219, 51, 51);
}

.risk .mb-20 {
  margin-bottom: 20px;
}
</style>
